#include "debug.h"
#include <algorithm>
#include "Utils\\ScreenManager.h"

NS_BEGIN(SDL)
NS_BEGIN(UTILS)

#pragma region CTOR & DTOR

ScreenManager::ScreenManager() : AManager<IScreen>()
{
	CTOR("ScreenManager", this);
}

ScreenManager::ScreenManager(ScreenManager const&)
{
}

ScreenManager::~ScreenManager()
{
	DTOR("ScreenManager", this);
}

#pragma endregion
#pragma region Operators

ScreenManager& ScreenManager::operator=(ScreenManager const&)
{
	return (*this);
}

#pragma endregion

void ScreenManager::Add(std::string const& _name, IScreen* _element,
						MngrFunc _func, IArgs* _datas, bool _delete)
{
	if ((*this)[_name] == NULL)
	{
		this->AManager<IScreen>::Add(_name, _element, _func, _datas, _delete);
		m_screenOrder.push_back(_name);
	}
}

void ScreenManager::Remove(std::string const& _name)
{
	this->AManager<IScreen>::Remove(_name);
	m_screenOrder.erase(std::find(m_screenOrder.begin(), m_screenOrder.end(), _name));
}

void ScreenManager::Clear()
{
	this->AManager<IScreen>::Clear();
	m_screenOrder.clear();
}

void ScreenManager::Manage(IArgs* _args)
{
	std::vector<std::string>::iterator it = m_screenOrder.begin();
	for (; it != m_screenOrder.end(); ++it)
	{
		if (m_elements[*it].second != NULL)
		{
			m_elements[*it].second(m_elements[*it].first, _args);
		}
	}
}

void ScreenManager::Exec(IArgs* _args)
{
	std::vector<std::string>::iterator it = m_screenOrder.begin();
	for (; it != m_screenOrder.end(); ++it)
	{
		if (m_elements[*it].second != NULL)
		{
			IScreen* s = m_elements[*it].first->Object;
			ScreenArgs* sa = (ScreenArgs*)m_elements[*it].first->Datas;
			if (sa->Pause == false)
				s->Exec(m_elements[*it].first->Datas);
		}
	}
}

void ScreenManager::Draw(IArgs* _args)
{
	std::vector<std::string>::iterator it = m_screenOrder.begin();
	for (; it != m_screenOrder.end(); ++it)
	{
		if (m_elements[*it].second != NULL)
		{
			IScreen* s = m_elements[*it].first->Object;
			ScreenArgs* sa = (ScreenArgs*)m_elements[*it].first->Datas;
			if (sa->Hide == false)
				s->Draw(m_elements[*it].first->Datas);
		}
	}
}

NS_END(UTILS)
NS_END(SDL)
